!pr2
!lm12
!rm75
Applesoft Program Locator...........................Bill Morgan

Have you ever wanted to know exactly where your Applesoft program and variables are in memory?  How much space is code and how much is variables?  How close you're getting to the Hi-Res display space?  FRE(0) will tell you how much space you have, but not where it is.  You can PEEK the Applesoft pointers, or go into the monitor to check them, but that means you have to remember where all the pointers are.

In the October, 1982 issue of Big Apple Users Digest I saw a program by Frank Weinberg to build an EXEC file called FPSTAT, which displays the Applesoft pointers to program and variable locations.  (That program was credited as being reprinted from The Grapevine, August, 1982.)  Now that was pretty neat, but EXEC is so slow, and the adresses were printed in decimal.  I'm more comfortable thinking of addresses in hex notation.  Bob suggested writing a BRUNnable program which would execute in page 2 (the input buffer), thus avoiding conflict with any page 3 routines that might be present.  Here's what I came up with.


Using LOCATOR

Whenever you want to know the memory situation, just BRUN LOCATOR.  It will display something like this:

!lm+10
PROGRAM: $0801 TO $0923
 SIMPLE: $0923 TO $0A35
 ARRAYS: $0A35 TO $1B3C
STRINGS: $9435 TO $9600
!lm-10

PROGRAM shows the location of the actual text of your program.  SIMPLE is the simple variables, both numeric and string pointers.  ARRAYS is the array variables, both numeric and string.  STRINGS is the area used by the actual text of the strings.

Notice that the upper addresses are all one too large.  Applesoft's end-of-program and end-of-variables pointers actually point to the next available location, rather than the last location used.  Similarly, the end-of-strings pointer is HIMEM, which is one past the last location available.  I wrote another version of LOCATOR which automatically decremented the second address in each line, but that got cumbersome, and returned silly values if the Applesoft program had not yet been RUN.  (For example, SIMPLE: $0923 TO $0922.)

If you want to CALL LOCATOR from within an Applesoft program, change line 1320 from JMP $3D0 to RTS, and change the origin to $294.  Then you can CALL 660, if you're not using very long input lines.  Or, you can put LOCATOR in page 3, if you're not already using that area.

It is also interesting to RUN a program, BRUN LOCATOR, then type FRE(0) and call LOCATOR again.  This lets you see just how much wasted string space you have had, and gives you some idea how long the garbage collector takes to clear how much space.

I'm looking forward to using LOCATOR together with EXAMINER (from AAL June, 1982) to study Applesoft's variable structure.  You can find more information on Applesoft variables and their pointers on pages 126-127 and 137 of the Applesoft manual.


How LOCATOR Works

Since we are printing eight addresses, the X-register is used to count from 0-7.  In lines 1140-1190 that count is converted into a value of $0, $8, $10, or $18, to determine which title line to print.  If the titles hadn't been a convenient 8 bytes long, we could have inserted a title offset at the beginning of each of the .DA statements in lines 1570-1600, and loaded Y from there.

The heart of the program is the table of Applesoft pointers at lines 1570-1600.  In lines 1420-1440 the Y-register is loaded with a value from the table, then used to load the A- and X-registers with the address pointed to.  The program then calls MON.PRNTAX, which displays first the A- and then the X-register.

